package com.nowadays.mobile.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.nowadays.mobile.R;
import com.nowadays.mobile.db.DbProvider;
import com.nowadays.mobile.model.Focus;
import com.nowadays.mobile.utils.FocusUtils;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends AppCompatActivity {

  private Handler handler;
  private static final String Tag = SplashActivity.class.getSimpleName();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    ImageView splashLogo = (ImageView) findViewById(R.id.splash_logo);
    final TextView splashNameTv = (TextView) findViewById(R.id.splash_name);
    final TextView splashTextTv = (TextView) findViewById(R.id.splash_text);

//    Animation translate = AnimationUtils.loadAnimation(this, R.anim.splash_loading);
    splashLogo.animate()
        .setDuration(1500)
        .translationYBy(FocusUtils.dip2px(this, 80))
        .setStartDelay(500)
        .setListener(new AnimatorListenerAdapter() {
          @Override
          public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            splashNameTv.animate().alphaBy(1.0f).setDuration(500);
            splashTextTv.animate().alphaBy(1.0f).setDuration(500);
            jump();
//            handler.sendEmptyMessage(SettingInfo.STEP);
          }
        });

    HandlerThread thread = new HandlerThread("saveFocus");
    thread.start();
    handler = new Handler(thread.getLooper()) {
      @Override
      public void handleMessage(Message msg) {
//                DbProvider.getInstance(SplashActivity.this).clearFocus();
        for (int i = 0; i < randomNums(25, 100, 0); i++) {
          saveFocus();
        }
        jump();
      }
    };
  }

  @Override
  protected void onResume() {
    super.onResume();
  }

  private void jump() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
      }
    }, 500);
  }

  private void saveFocus() {
    Focus focus = new Focus();
    long now = System.currentTimeMillis() / FocusUtils.MILLIS_IN_SECOND;
    int duration = randomNums(1, 60, 5) * 60; //秒
    int startTime = (int) now - randomTime(); //秒
    int endTime = startTime + duration; //秒
    focus.setStartTime(startTime);
    focus.setEndTime(endTime);
    focus.setDuration(duration);
    DbProvider.getInstance(SplashActivity.this).saveFocus(focus);
  }

  private int randomTime() {
    Random r = new Random();
    int day = 60 * 60 * 24;
    int month = day * 30;
    int year = month * 12;
    return r.nextInt(year);
  }

  int randomNums(int low, int high, int multi) {
    Random r = new Random();
    return r.nextInt(high - low) + low;
//        if (multi > 0) {
//            nums = nums / multi * multi;
//            if (nums == 0) {
//                nums = multi;
//            }
//        }
  }

}
